import * as Cesium from "cesium";
// 工具函数
// 更新3dtiles的模型矩阵
export const update3dtilesMaxtrix = (tx, ty, tile) => {
  const cartographic = Cesium.Cartographic.fromCartesian(
    tile.boundingSphere.center
  );
  const surface = Cesium.Cartesian3.fromRadians(
    cartographic.longitude,
    cartographic.latitude,
    cartographic.height
  );
  const m = Cesium.Transforms.eastNorthUpToFixedFrame(surface);
  const _tx = tx ? tx : 0;
  const _ty = ty ? ty : 0;
  const _tz = 430;
  const tempTranslation = new Cesium.Cartesian3(_tx, _ty, _tz);
  const offset = Cesium.Matrix4.multiplyByPoint(
    m,
    tempTranslation,
    new Cesium.Cartesian3(0, 0, 0)
  );
  const translation = Cesium.Cartesian3.subtract(
    offset,
    surface,
    new Cesium.Cartesian3()
  );
  tile.modelMatrix = Cesium.Matrix4.fromTranslation(translation);
};

// 渲染3dtiles shader
export const renderLightEffect = (
  vertexShaderText,
  fragmentShaderText,
  tileset
) => {
  const radius = tileset.boundingSphere.radius;
  const params = {
    varyings: {
      v_normalMC: Cesium.VaryingType.VEC3,
      v_color: Cesium.VaryingType.VEC3,
      v_Itime: Cesium.VaryingType.FLOAT,
    },
    uniforms: {
      nightTexture: {
        value: new Cesium.TextureUniform({
          url: "/src/assets/wall.png",
        }),
        type: Cesium.UniformType.SAMPLER_2D,
      },
      u_envTexture2: {
        value: new Cesium.TextureUniform({
          url: "/src/assets/pic.jpg",
        }),
        type: Cesium.UniformType.SAMPLER_2D,
      },
      maxHeight: {
        type: Cesium.UniformType.FLOAT,
        value: 500.0,
      },
      minHeight: {
        type: Cesium.UniformType.FLOAT,
        value: 0.0,
      },
      // 扫描线条的颜色z
      u_scanerColor: {
        type: Cesium.UniformType.VEC3,
        value: Cesium.Color.WHITE,
      },
      // 基座颜色
      u_baseColor: {
        type: Cesium.UniformType.VEC3,
        // new Cesium.Color(36, 172, 242)
        value: new Cesium.Color(36 / 255, 172 / 255, 242 / 255),
      },
      // 渐变顶部颜色
      u_headColor: {
        type: Cesium.UniformType.VEC3,
        value: Cesium.Color.WHITE,
      },
      // 扫描的位置
      u_min: {
        type: Cesium.UniformType.FLOAT,
        value: -radius / 2.0,
      },
      u_max: {
        type: Cesium.UniformType.FLOAT,
        value: radius / 2.0,
      },
      // 扫光颜色
      u_live_color: {
        type: Cesium.UniformType.VEC3,
        value: new Cesium.Color(0.99, 0.99, 0.99),
      },
      // 光带宽度
      u_live_width: {
        type: Cesium.UniformType.FLOAT,
        value: 300.0,
      },
    },
    mode: Cesium.CustomShaderMode.MODIFY_MATERIAL,
    lightingModel: Cesium.LightingModel.PBR,
  };
  if (vertexShaderText) {
    params.vertexShaderText = vertexShaderText;
  }
  if (fragmentShaderText) {
    params.fragmentShaderText = fragmentShaderText;
  }
  const customShader = new Cesium.CustomShader(params);
  tileset.customShader = customShader;
  const maxHeight = 240;
  const minHeight = 60;
  tileset.customShader.setUniform("maxHeight", maxHeight);
  tileset.customShader.setUniform("minHeight", minHeight);
};
